package com.itheima.reggie.controller;

import com.itheima.reggie.common.OssClinet;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/common")
public class CommonController {
//    @Value("${reggie.path}")
//    private String bashPath;
    @Autowired
    private OssClinet ossClinet;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file)  {
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuidname = UUID.randomUUID().toString().replace("-","");
        String Filename=uuidname+suffix;
        //设置文件夹路径
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String datestr = dateFormat.format(new Date());
        Filename=datestr+"/"+Filename;
        String url=null;
        try {
             url = ossClinet.onload(Filename,(FileInputStream)file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            file.transferTo(new File(bashPath+Filename));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return R.success(url);
    }
   // @GetMapping("/download")
   /* public void download(String name, HttpServletResponse response){
        try {
            FileInputStream is = new FileInputStream(new File(bashPath+name));//路径加文件名
            ServletOutputStream os = response.getOutputStream();
            response.setContentType("imag/jepg");
            byte [] bytes=new byte[1024];
            int len=0;
            while ((len=is.read(bytes))!=-1){
                os.write(bytes,0,len);
                os.flush();
            }
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
//    /**
//     * 文件上传
//     * @param file
//     * @return
//     */
//    @PostMapping("/upload")
//    public R upload(MultipartFile file){
//        String suffex = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//        String name = UUID.randomUUID().toString().replace("-", "");
//        String fileName=name+suffex;
//        File path = new File(bashPath);
//        if (!path.exists()){
//            path.mkdirs();
//        }
//        try {
//            file.transferTo(new File(fileName));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return R.success(bashPath+fileName);
//    }
//
//    /**
//     * 文件下载
//     * @param name
//     * @param response
//     */
//    @GetMapping("/download")
//    public void download(String name,HttpServletResponse response) {
//        FileInputStream is = null;
//        ServletOutputStream os = null;
//        response.setContentType("image/jepg");
//
//        try {
//            is = new FileInputStream(name);
//            os = response.getOutputStream();
//            int len = 0;
//            byte[] bytes = new byte[1024];
//            while (true) {
//                if (!((len = is.read(bytes)) != -1)) break;
//                os.write(bytes, 0, len);
//                os.flush();
//
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {if(os!=null){
//                os.close();}
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            try {
//                if (is!=null){
//                    is.close();
//                }
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}